﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DTO;
using BLL;

namespace HotelManagement.Control.CMS
{
    public partial class UcMenuLeft : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string[] temp = base.Request.Path.Split(new char[] { '?' });
            this.active = this.SetActive(temp[0].ToLower());
        }
        protected int active = 0;
        private static DataTable tblMenu = null;

        // Methods
        private void AddRowInTable(DataTable tbl)
        {
            try
            {
                if (tblMenu == null)
                {
                    tblMenu = this.CreateTable();
                }
                int iactive = (tblMenu.Rows.Count != 0) ? (Convert.ToInt32(tblMenu.Rows[tblMenu.Rows.Count - 1]["IDActive"]) + 1) : tblMenu.Rows.Count;
                for (int i = 0; i < tbl.Rows.Count; i++)
                {
                    DataRow myrow = tblMenu.NewRow();
                    myrow["IDActive"] = iactive.ToString();
                    myrow["IDMenuChild"] = tbl.Rows[i]["IDFormSub"];
                    tblMenu.Rows.Add(myrow);
                }
            }
            catch
            {
            }
            finally
            {
                tbl.AcceptChanges();
            }
        }

        private DataTable CreateTable()
        {
            DataTable tbl = null;
            try
            {
                tbl = new DataTable();
                DataColumn col = new DataColumn("IDActive");
                DataColumn col1 = new DataColumn("IDMenuChild");
                tbl.Columns.Add(col);
                tbl.Columns.Add(col1);
            }
            catch
            {
                tbl = null;
            }
            return tbl;
        }

        protected DataTable LoadMenuCapCon(string strIDCapCha)
        {
            DataTable tbl = null;
            try
            {
                Sys_Page sp = new Sys_Page();
                sp.IdPage = Convert.ToInt32(strIDCapCha);
                tbl = Sys_PageBLL.GetSubMenu(sp);
                this.AddRowInTable(tbl);
            }
            catch
            {
                tbl = null;
            }
            return tbl;
        }

        public void LoadMenu(int id)
        {
            DataTable tbl = null;
            try
            {
                Sys_Page sp1 = new Sys_Page();
                sp1.IdModule = id;
                tbl = Sys_PageBLL.LoadMenuLeft(sp1);
                tblMenu = this.CreateTable();
            }
            catch
            {
                tbl = null;
            }
            finally
            {
                this.rptParentMenu.DataSource = tbl;
                this.rptParentMenu.DataBind();
            }
        }
           private int SetActive(string strlinkForm)
            {
                int iVal = 0;
                try
                {
                    Sys_Page sp2 = new Sys_Page();
                    
                    if (string.IsNullOrEmpty(strlinkForm))
                    {
                        return 0;
                    }
                    string link = strlinkForm.Substring(strlinkForm.LastIndexOf("/") + 1);
                    sp2.Link = link;
                    DataTable dt = new DataTable();
                    dt=Sys_PageBLL.SetActiveMenu(sp2);
                    if (dt.Rows.Count > 0)
                    {
                        string strTemp = (string)dt.Rows[0][0].ToString();
                        for (int i = 0; i < tblMenu.Rows.Count; i++)
                        {
                            if (tblMenu.Rows[i]["IDMenuChild"].ToString() == strTemp)
                            {
                                return Convert.ToInt16(tblMenu.Rows[i]["IDActive"].ToString());
                            }
                        }
                    }
                }
                catch
                {
                    iVal = 0;
                }
                return iVal;
            }
    }
}